#ifndef _LOGGER_H_
#define _LOGGER_H_
#include"message.h"
#include"public.h"

#include<fstream>


class Logger{
private:
 Logger();
 Logger(const Logger& ) = delete;
 Logger & operator=(const Logger& ) =delete;//单例
 bool LogToTerminal_;//打印到终端
 bool LogToLogfile_;//打印到文件
 LOG_LEVEL logLevel_;//日志级别
 std::fstream FileStream_;
 
 public:
 static Logger& GetInstance();

 void SetLogToTermianl(bool );
 bool GetLogToTerminal()const;

 void SetLogTOLogfile(bool);
 bool GetLogToLogfile() const;

 void SetLogLevel(LOG_LEVEL);
 LOG_LEVEL GetLogLevle() const;

 void SetLogFileName(const std::string& logFileName = "dsf.log");
 void operator+=(const Message & message)//双目重载运算符要写在类内
 {
   if(LogToTerminal_)
   {
    std::cout<<message.ToString(true)<<std::endl;
   }
   if(LogToLogfile_)
   {
    FileStream_<<message.ToString(false);
   }
    if(message.ToString().find("FATAL") != std::string::npos)//通过查找message中是否有FATAL来判断是否要终止程序,不能通过m_level来判断,因为可能m_level为INFO,但是程序员想打印其它级别的日志,string.nops为末尾
    {
        std::cout<<"FATAL HEPPENED!!!"<<std::endl;
        exit(-1);
    }
 }
};
#endif

#define  LOG(level)\
      if(level>=Logger::GetInstance().GetLogLevle())\
      Logger::GetInstance() += Message(level,__FILE__,__FUNCTION__,__LINE__)

#define LOG_INFO  LOG(INFO)
#define LOG_ERROR LOG(ERROR)
#define LOG_DEBUG LOG(DEBUG)
#define LOG_FATAL LOG(FATAL)